package com.clearance.mine_clearance.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.entry.*;
import com.clearance.mine_clearance.entry.vo.InviterVo;
import com.clearance.mine_clearance.entry.vo.TgUserVo;
import com.clearance.mine_clearance.mapper.CapitalMapper;
import com.clearance.mine_clearance.mapper.ConfigMapper;
import com.clearance.mine_clearance.mapper.InviterMapper;
import com.clearance.mine_clearance.mapper.TgUserNapper;
import com.clearance.mine_clearance.untils.RedisUntils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Year;
import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin("*")
@RestController
@RequestMapping("/mine/user")
public class UserController {
    @Autowired
    private TgUserNapper tgUserNapper;
    @Autowired
    private CapitalMapper capitalMapper;
    @Autowired
    private InviterMapper inviterMapper;
    @PostMapping("/selectUserList")
    public Result selectUserList(@RequestBody TgUserVo tgUserVo) {
        Page<TgUser> tgUserPage = new Page<>(tgUserVo.getCurrent(), tgUserVo.getSize());
        LambdaQueryWrapper<TgUser> tgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (tgUserVo.getType() != null) {
            tgUserLambdaQueryWrapper.eq(TgUser::getType, tgUserVo.getType());
        }
        if (StringUtils.isNotEmpty(tgUserVo.getFirstName())) {
            tgUserLambdaQueryWrapper.eq(TgUser::getFirstName, tgUserVo.getFirstName());
        }
        if (StringUtils.isNotEmpty(tgUserVo.getUserName())) {
            tgUserLambdaQueryWrapper.eq(TgUser::getUserName, tgUserVo.getUserName());
        }
        if (StringUtils.isNotEmpty(tgUserVo.getTgId())) {
            tgUserLambdaQueryWrapper.eq(TgUser::getTgId, tgUserVo.getTgId());
        }
        if (tgUserVo.getStartTime() != null && tgUserVo.getEndTime() != null) {
            tgUserLambdaQueryWrapper.between(TgUser::getCreateTime, tgUserVo.getStartTime(), tgUserVo.getEndTime());
        }
        tgUserLambdaQueryWrapper.orderByDesc(TgUser::getCreateTime);
        Page<TgUser> tgUserPage1 = tgUserNapper.selectPage(tgUserPage, tgUserLambdaQueryWrapper);
        return new Result().success(tgUserPage1);
    }

    //修改用户;
    @PostMapping("/updateUser")
    public Result updateBlanceOf(@RequestBody TgUser tgUser) {
        TgUser tgUserSel = tgUserNapper.selectById(tgUser.getId());
        if (tgUserSel == null) {
            return new Result().error("不存在当前的用户信息;");
        }
        tgUserNapper.updateById(tgUser);
        TgUser selectById = tgUserNapper.selectById(tgUser.getId());
        RedisUntils.setValue(Constants.TGUSER + selectById.getTgId(), JSON.toJSONString(selectById));
        return new Result().success(tgUser);
    }

    /**
     * 添加用户;
     */
    @PostMapping("/addTgUser")
    public Result addTgUser(@RequestBody TgUser tgUser) {
        TgUser tgUserSel = new TgUser();
        tgUserSel.setType(1);
        tgUserSel.setIsBot(1);
        tgUserSel.setCreateTime(new Date());
        tgUserSel.setUserName(tgUser.getUserName());
        tgUserSel.setFirstName(tgUser.getFirstName());
        tgUserSel.setBalance(tgUser.getBalance());
        tgUserNapper.insert(tgUserSel);
        return new Result().success(tgUserSel);
    }

    @PostMapping("/delectList")
    public Result delect(@RequestBody TgUserVo tgUserVo) {
        if (tgUserVo.getDeletList() == null || tgUserVo.getDeletList().isEmpty()) {
            return new Result().error("缺少用户信息;");
        }
        List<Integer> deletList = tgUserVo.getDeletList();
        for (Integer integer : deletList) {
            tgUserNapper.deleteById(integer);
        }
        return new Result().success(tgUserVo);
    }


    @PostMapping("/updateBlanceOf")
    public Result updateBlanceOf(@RequestBody TgUserVo tgUserVo) {
        //4 上分 5 下分
        if (tgUserVo.getRechargeType() == null) {
            return new Result().error("请选择上分或者下分");
        }
        if (tgUserVo.getBalance() == null) {
            return new Result().error("缺少上下分金额;");
        }
        TgUser tgUser = tgUserNapper.selectById(tgUserVo.getId());
        if (tgUser == null) {
            return new Result().error("不存在当前的账户信息;");
        }
        //4 上分 5 下分
        BigDecimal balance = tgUser.getBalance();
        if (tgUserVo.getRechargeType().equals(4)) {
            tgUser.setBalance(tgUser.getBalance().add(tgUserVo.getBalance()));
        } else if (tgUserVo.getRechargeType().equals(5)) {
            tgUser.setBalance(tgUser.getBalance().subtract(tgUserVo.getBalance()));
        } else {
            return new Result().error("上下分类型错误;");
        }
        tgUserNapper.updateById(tgUser);
        //更新redis缓存;
        RedisUntils.setValue(Constants.TGUSER + tgUser.getTgId(), JSON.toJSONString(tgUser));
        //添加记录;
        Capital capital = new Capital();
        capital.setTgUserId(tgUser.getId());
        capital.setAmount(tgUserVo.getBalance());
        capital.setType(tgUserVo.getRechargeType());
        capital.setUserName(tgUser.getUserName());
        capital.setFirstName(tgUser.getFirstName());
        capital.setAmount(tgUserVo.getBalance());
        capital.setBeforeAmount(balance.setScale(2, RoundingMode.UP));
        capital.setAfterAmount(tgUser.getBalance().setScale(2,RoundingMode.UP));
        capital.setTime(new Date());
        capitalMapper.insert(capital);
        return new Result().success(tgUser);
    }
    //查询我的下级用户;

    @PostMapping("/getLastUser")
    public Result getLastUser(@RequestBody InviterVo inviterVo){
        Page<Inviter> inviterPage = new Page<>(inviterVo.getCurrent(),inviterVo.getSize());
        LambdaQueryWrapper<Inviter> inviterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inviterLambdaQueryWrapper.eq(Inviter::getInviterTgId,inviterVo.getThUserId());
        Page<Inviter> selectPage = inviterMapper.selectPage(inviterPage, inviterLambdaQueryWrapper);
        List<Inviter> selectPageRecords = selectPage.getRecords();
        List<TgUser> tgUserList = null;
        if(selectPageRecords!=null&&selectPageRecords.size()>0){
            List<Integer> collect = selectPageRecords.stream().map(Inviter::getBeInviteTgId).collect(Collectors.toList());
            LambdaQueryWrapper<TgUser> tgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tgUserLambdaQueryWrapper.in(TgUser::getId,collect);
            tgUserList = tgUserNapper.selectList(tgUserLambdaQueryWrapper);
        }else {
            tgUserList = new ArrayList<>();
        }
        Map<String,Object> result = new HashMap<>();
        result.put("total",selectPage.getTotal());
        result.put("tgUserList",tgUserList);
        return new Result().success(result);
    }

}










































